//
//  MarqueeView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/29.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class MarqueeView: UIView {

    let firstLabel = UILabel()
    let secondLabel = UILabel()
    
    override init(frame:CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.marqueeColor()
        
        firstLabel.text = marqueeText
        firstLabel.textColor = UIColor.red
        firstLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        
        secondLabel.text = marqueeText
        secondLabel.textColor = UIColor.red
        secondLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        
        
        self.addSubview(firstLabel)
        self.addSubview(secondLabel)
        
        firstLabel.frame = .init(x: 0, y: 0, width: marqueeText.size(font: RATIO(maxNum: 14), height: RATIO_H(maxNum: 40)).width, height: RATIO_H(maxNum: 40))
        
        secondLabel.frame = .init(x: firstLabel.frame.maxX + RATIO(maxNum: 20), y: 0, width: firstLabel.frame.size.width, height: RATIO_H(maxNum: 40))
        
//        firstLabel.snp.makeConstraints { (make) in
//            make.left.equalToSuperview()
//            make.height.equalToSuperview()
//            make.centerY.equalToSuperview()
//            make.width.greaterThanOrEqualTo(10)
//        }
//
//        secondLabel.snp.makeConstraints { (make) in
//            make.left.equalTo(firstLabel.snp.right).offset(RATIO(maxNum: 20))
//            make.height.equalToSuperview()
//            make.centerY.equalToSuperview()
//            make.width.greaterThanOrEqualTo(10)
//        }
        
        addAnimate()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 添加动画
    fileprivate func addAnimate() {
        
//        self.layoutIfNeeded()
        
        let firstW = firstLabel.frame.size.width

        UIView.animate(withDuration: 10.0, delay: 0, options: .curveLinear, animations: {

            self.firstLabel.frame.origin.x = -firstW
            self.secondLabel.frame.origin.x = 0

        }) { (finish) in

            self.firstLabel.frame.origin.x = 0
            self.secondLabel.frame.origin.x = firstW + 20

            self.addAnimate()
        }
        
    }
    
}
